<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AuthController extends Controller
{
	/**
	 * 参数字段解析
	 *
	 * @var        array
	 */
	protected $attributes = [
    	'username' => '用户名',
    	'password' => '密码'
    ];

    /**
     * 登陆
     *
     * @param      \Illuminate\Http\Request  $request  The request
     *
     * @return     <type>                    ( description_of_the_return_value )
     */
    public function login(Request $request){
    	$rules = [
    		'username' => 'required',
    		'password' => 'required'
    	];

    	$this->validate($rules);

    	if($token = auth('api')->attempt(['username' => $request->username,'password' => $request->password])){
    		return formatJsonResponse(['token' => $token]);
    	}

    	return formatJsonResponse(null,'用户或密码错误',10001);
    }


    public function permissions(){
        $userModel = app(\App\Models\User::class);

        return formatJsonResponse($userModel->hasPermission());
    }
}

